<?php

namespace app\beautiful\model\member;

use app\common\model\CommonModel;

use app\beautiful\model\project\OnceCardModel;

class MemberOnceCardModel extends CommonModel{

    protected  $name = 'beautiful_member_once_card';

    protected  $pk   = 'member_once_card_id';

    protected $type=[

        'pay_info' => 'array',

    ];

    

    public function oncecard(){

        return $this->hasOne(OnceCardModel::class,'once_card_id','once_card_id');

    }

    

    public function member()

    {

        return $this->hasOne(MemberModel::class,'member_id','member_id');

    }



    public function items()

    {

        return $this->hasMany(MemberOnceCardItemModel::class,'member_once_card_id','member_once_card_id');

    }



    public function getStatusMeansAttr($value,$data)

    {

        $today=date("Y-m-d");

        if($data['status']==8) return '已完成';

        if($data['status']==1 && $data['valid_end_time']>=$today) return '待消费';

        if($data['status']==1 && $data['valid_end_time']<$today) return '已过期';

        return '状态异常';

    }

    //前台需要

    public function getStatusAnotherAttr($value,$data){

        $today=date("Y-m-d");

        if($data['status'] == 8) return 8;

        if($data['status']==1 && $data['valid_end_time']>=$today) return 1;

        return -1;

    }



    public function getPayTimeFormatAttr($value,$data){

        return $data['pay_time'] ? date('Y-m-d H:i:s',$data['pay_time']) : '-';

    }



    public function getPayTypeMeanAttr($value,$data)

    {

        $type_means=[

            'weixin'=>'微信支付',

            'money'=>'余额支付',

            'offline' => '代客下单',

        ];

        return $type_means[$data['pay_type']] ?? '-';

    }

}